/* 
 * File:   Lesson6.cpp
 * Author: jose
 * 
 * Created on March 29, 2012, 2:33 PM
 */

#include "Lesson6.h"
#include "SdlUtils.h"
#include <iostream>
#include <SDL/SDL_image.h>

using namespace std;

Lesson6::Lesson6() {
}

Lesson6::Lesson6(const Lesson6& orig) {
}

Lesson6::~Lesson6() {
}

bool Lesson6::init(){
    
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return false;
    
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if(screen == NULL)
        return false;
    
    SDL_WM_SetCaption("Clipping", NULL);
    
    return true;
}

int Lesson6::Run(){
    
    bool quit = false;
    SDL_Event event;
    
    
    if( init() == false)
    {
        cout << "Could not init" << endl;
        return 1;
    } 
    
    message = SdlUtils::loadImageColorKey("dots.png");
    
    if(message == NULL)
    {
        cout << "Could not load the image" << endl;
        return 1;
    }
 
    //Clip range for the top left
    clip[ 0 ].x = 0;
    clip[ 0 ].y = 0;
    clip[ 0 ].w = 100;
    clip[ 0 ].h = 100;

    //Clip range for the top right
    clip[ 1 ].x = 100;
    clip[ 1 ].y = 0;
    clip[ 1 ].w = 100;
    clip[ 1 ].h = 100;

    //Clip range for the bottom left
    clip[ 2 ].x = 0;
    clip[ 2 ].y = 100;
    clip[ 2 ].w = 100;
    clip[ 2 ].h = 100;

    //Clip range for the bottom right
    clip[ 3 ].x = 100;
    clip[ 3 ].y = 100;
    clip[ 3 ].w = 100;
    clip[ 3 ].h = 100;
    
    
    //Fill the screen white 
    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

    
    SdlUtils::applySurface(0,0, message, screen, &clip[0]);
    SdlUtils::applySurface(540,0, message, screen, &clip[1]);
    SdlUtils::applySurface(0,380, message, screen, &clip[2]);
    SdlUtils::applySurface(540,380, message, screen, &clip[3]);

    if(SDL_Flip(screen) == -1)
    {
        return 1;
    }
  
    while(!quit)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
    }
    
    SDL_FreeSurface( message );

    SDL_Quit();
    
    return 0;
     
}